$(function () {
    initValue('init');
    getData();
    $('body').on('change', '#search_input', function () {
        var search = $("#search").val();
        search_input(search);
        getData('add');
    }).on('keyup', '#search_input', function () {
        if (event.keyCode == "13") {
            var search = $("#search").val();
            search_input(search);
            getData('add');
        }
    }).on("click", "#delete", function () {
        var _this = $(this);
        var value = _this.data('value');
        var overallValue = _getLOCALSTORAGE("overallKey");
        var index = overallValue.indexOf("" + value + "");
        if (index > -1) {
            overallValue.splice(index, 1);
        }
        _setLOCALSTORAGE("overallKey", overallValue);
        $("#" + value).remove();
        getData('delete');
    }).on("click", "#deleteAll", function () {
        _removeLOCALSTORAGE("overallKey");
        getData('delete');
    })
});

function getData(type) {
    initValue(type);
    getDataList("course");
    getDataList("teacher");
    getDataList("article");
    getDataList("examPaper");
}

function search_input(search) {
    var overallValue = _getLOCALSTORAGE("overallKey");
    if (isEmpty(overallValue) && !isEmpty(search)) {
        overallValue = [search];
        _setLOCALSTORAGE("overallKey", overallValue);
    } else {
        var index = overallValue.indexOf("" + search + "");
        if (index <= -1 && !isEmpty(search)) {
            overallValue.push(search);
            _setLOCALSTORAGE("overallKey", overallValue);
        }
    }
}

function initValue(type) {
    if ('init' == type || 'add' == type) {
        var search = $("#search").val();
        search_input(search);
    }
    var overallValue = _getLOCALSTORAGE("overallKey");
    var html = '';
    if (!isEmpty(overallValue)) {
        overallValue.forEach((item, index, array) => {
            html += '<p class="qt-label-close" id=' + item + '><a href="#" class="button button-dark">' + item + '</a><i class="icon iconfont icon-guanbi" id="delete" data-value=' + item + '></i></p>';
        });
        _setLOCALSTORAGE("overallKey", overallValue);
    }
    $(".values").html(html);
}

function getDataList(type) {
    var overallValue = _getLOCALSTORAGE("overallKey");
    if (isEmpty(overallValue)) {
        overallValue = "";
    } else {
        overallValue = overallValue.join(',')
    }
    var url = '';
    if ("course" == type) {
        url = baseloaction + '/mobile/globalSearch/globalSearchCourse';
    } else if ("teacher" == type) {
        url = baseloaction + '/mobile/globalSearch/globalSearchTeacher';
    } else if ("examPaper" == type) {
        url = baseloaction + '/mobile/globalSearch/globalSearchExamPaper';
    } else {
        url = baseloaction + '/mobile/globalSearch/globalSearchArticle';
    }
    $.ajax({
        url: url,
        data: {'overallValue': overallValue},
        type: 'post',
        dataType: "text",
        success: function (result) {
            $("#" + type + "Context").html(result)
        },
        error: function (error) {
            var _html = '<section class="no-data__wrap"><span class="no-data-ico"></span><p class="mt20"><span class="c-666">没有搜索到相关数据</span></p></section>'
            $("#" + type + "context").html(_html);
        }
    })
}

var isEmpty = function (str) {
    if (str === null || typeof str === 'undefined' || str === '') {
        return true
    }
    return false
}